from ui.components.base.widget import TgBaseWidget
from ui.components.layouts.vbox_layout import TgVBoxLayout
from ui.components.inputs.label import TgLabel
from ui.components.charts.html_table import TgHtmlTable
from PySide6.QtWidgets import QScrollArea
from loguru import logger


class GlobalIndexView(TgBaseWidget):
    
    def __init__(self):
        super().__init__()
        self.layout = TgVBoxLayout(self)
        self._setup_ui()
        
    def _setup_ui(self):
        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)

        content_widget = TgBaseWidget()
        content_layout = TgVBoxLayout(content_widget)

        self._create_hk_table(content_layout)
        content_layout.addSpacing(20)
        self._create_global_table(content_layout)
        content_layout.addStretch(1)

        scroll_area.setWidget(content_widget)
        self.layout.addWidget(scroll_area)

    def _create_hk_table(self, layout):
        layout.addWidget(TgLabel("港股指数"))
        
        hk_headers = ["指数代码", "指数名称", "近30天走势", "近一日", "近一周", "近三周", "近一月", "近2月", "近3月", "近1年", "年初至今"]
        self.hk_table = TgHtmlTable(headers=hk_headers, cell_size='small')
        self.hk_table.setFixedHeight(200)
        
        self.hk_table.set_profit_loss_style(list(range(3, 11)), use_gradient=True, color_type='background')
        self.hk_table.set_chart_column(2, {
            'width': 80, 
            'height': 30, 
            'color_style': 'stock_profit'
        })
        self.hk_table.enable_sorting(enable=True)
        
        layout.addWidget(self.hk_table)

    def _create_global_table(self, layout):
        layout.addWidget(TgLabel("全球指数"))
        
        global_headers = ["指数代码", "指数名称", "近30天走势", "近一日", "近一周", "近三周", "近一月", "近2月", "近3月", "近1年", "年初至今"]
        self.global_table = TgHtmlTable(headers=global_headers, cell_size='small')
        self.global_table.setFixedHeight(300)
        
        self.global_table.set_profit_loss_style(list(range(3, 11)), use_gradient=True, color_type='background')
        self.global_table.set_chart_column(2, {
            'width': 80, 
            'height': 30, 
            'color_style': 'stock_profit'
        })
        self.global_table.enable_sorting(enable=True)
        
        layout.addWidget(self.global_table)

    def update_hk_data(self, data):
        self.hk_table.update_data(data=data)

    def update_global_data(self, data):
        self.global_table.update_data(data=data)